/*
 * ProjetoPSA - Plano de Substituição de Aulas
 *    Copyright (C) 2010  Marcelo e Cicero
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetoPSA.facades;

import java.util.List;
import java.util.logging.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import projetoPSA.hibernate.DaoGenerico;
import projetoPSA.pojos.*;

/**
 *
 * @author Marcelo e Cicero
 */
public class ProfessorFacade {

    private Session sessao;
    private Professor professor;
    private Criteria criteria;
    private Logger log = Logger.getLogger(this.getClass().getName());

    public ProfessorFacade(Professor professor) {
        this.professor = professor;
    }

    public ProfessorFacade() {
        begin();
    }

    private void begin() {
        this.sessao = new DaoGenerico().getSessao();
        this.criteria = sessao.createCriteria(Professor.class);
        log.info("Iniciando Sessão!");
    }

    private void commit() {
        this.sessao.beginTransaction().commit();
        log.info("Efetuando commit...");
        this.sessao.flush();
        this.sessao.close();
        log.info("Fechando sessao!");
    }

    public Professor getProfessor(Long id) {
        log.info("Carregando o professor...");
        Professor ret = (Professor) criteria.add(Expression.eq("id", id)).uniqueResult();
        commit();
        return ret;
    }
    public List<Professor> getProfessores() {
        log.info("Carregando o professores...");
        List<Professor> ret = criteria.list();
        return ret;
    }

    public void persist() {
        begin();
        this.sessao.saveOrUpdate(this.professor);
        commit();
    }
    public void delete() {
        begin();
        this.sessao.delete(this.professor);
        commit();
    }
}
